#include "../source/sylar.h"
#include <vector>
sylar::Logger::ptr g_logger = SYLAR_LOG_ROOT();

void run_in_fiber() {
    SYLAR_LOG_INFO(g_logger) << "run_in_fiber beigin";

    sylar::Fiber::YieldToHold();

    SYLAR_LOG_INFO(g_logger) << "run_in_fiber end";

    sylar::Fiber::YieldToHold();

    SYLAR_LOG_INFO(g_logger) << "run_in_fiber other";

}

void test_fiber() {
    sylar::Fiber::GetThis();
    SYLAR_LOG_INFO(g_logger) << "main beign...";
    sylar::Fiber::ptr fiber(new sylar::Fiber(run_in_fiber));
    fiber->swapIn();
    SYLAR_LOG_INFO(g_logger) << "main after swapIn";
    fiber->swapIn();
    SYLAR_LOG_INFO(g_logger) << "main after end";
    fiber->swapIn();
    // fiber->swapIn();

    // SYLAR_LOG_INFO(g_logger) << "main other";

    
}

int main(int argc, char** argv) {
    
    
    sylar::Thread::SetName("main");
    SYLAR_LOG_INFO(SYLAR_LOG_ROOT()) << "test fiber...";

    std::vector<sylar::Thread::ptr> thrs;
    for(int i=0;i<3;++i) {
        thrs.push_back(sylar::Thread::ptr(new sylar::Thread(&test_fiber, "thread_"+std::to_string(i))));
    }
    for(auto& i : thrs) {
        i->join();
    }

    SYLAR_LOG_INFO(g_logger) << "end test...";
    
    return 0;
}